﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class Process
    {
        public static DataTable ExecuteQuery(String sql)
        {
            if (Provider.cnn.State == ConnectionState.Closed)
                Provider.cnn.Open();
            DataTable dt = new DataTable();
            SqlCommand command = new SqlCommand();
            command.CommandText = sql;
            command.Connection = Provider.cnn;
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt);
            da.Dispose();
            command.Dispose();
            Provider.cnn.Close();
            return dt;
        }

        public static DataSet OpenDataSet(String sql, string tenbang)
        {
            if (Provider.cnn.State == ConnectionState.Closed)
                Provider.cnn.Open();
            DataSet ds = new DataSet();
            SqlCommand command = new SqlCommand();
            command.CommandText = sql;
            command.Connection = Provider.cnn;
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(ds, tenbang);
            da.Dispose();
            command.Dispose();
            Provider.cnn.Close();
            return ds;
        }

        public static bool ExecuteNonQuery(String sql)
        {
            if (Provider.cnn.State == ConnectionState.Closed)
                Provider.cnn.Open();
            bool kq;
            SqlCommand cmd = Provider.cnn.CreateCommand();
            cmd.CommandText = sql;
            try
            {
                cmd.ExecuteNonQuery();
                kq = true;
            }
            catch
            {
                kq = false;
            }
            cmd.Dispose();
            Provider.cnn.Close();
            return kq;
        }

        public static bool ExecuteStoreProceduce(String sql, String storeProceduceName)
        {
            if (Provider.cnn.State == ConnectionState.Closed)
                Provider.cnn.Open();
            bool kq;
            SqlCommand cmd = Provider.cnn.CreateCommand();
            cmd.CommandText = sql;
            try
            {
                cmd.ExecuteNonQuery();
                kq = true;
            }
            catch
            {
                kq = false;
            }
            cmd.Dispose();
            Provider.cnn.Close();
            return kq;
        }

        public static int identityNumber(String tableName)
        {
            string sql = "select * from " + tableName;
            DataTable dt = ExecuteQuery(sql);
            int identityNum = 1;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (int.Parse(dt.Rows[i][0].ToString()) != identityNum)
                {
                    return identityNum;
                }
                identityNum++;
            }
            return identityNum;
        }
    }
}
